package com.mvetc.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.mvetc.entity.Movie;
import com.mvetc.entity.PageInfo;
import com.mvetc.service.MovieService;
import com.mvetc.service.impl.MovieServiceImpl;

/**
 * Servlet implementation class MovieServlet
 */
@WebServlet("/MovieServlet")
@MultipartConfig
public class MovieServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private MovieService movieService = new MovieServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MovieServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if (op == null || "getMovieList".equals(op)) {
			doGetMovieList(request,response);
		}else if ("deleteByMovieId".equals(op)) {
			doDeleteByMovieId(request,response);
		}else if ("updateByMovieId".equals(op)) {
			doUpdateByMovieId(request,response);
		}else if ("addMovieObject".equals(op)) {
			doAddMovie(request,response);
		}
	}

	private void doAddMovie(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String json = request.getParameter("addMovie");
		Gson gson = new Gson();
		Movie fromJson = gson.fromJson(json, Movie.class);
		movieService.add(fromJson);
		
		doGetMovieList(request, response);
	}

	private void doUpdateByMovieId(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String json = request.getParameter("updateMovie");
		Gson gson = new Gson();
		Movie fromJson = gson.fromJson(json, Movie.class);
		movieService.modify(fromJson);
		
		doGetMovieList(request, response);
	}

	private void doDeleteByMovieId(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String movieId = request.getParameter("movieId");
		movieService.deleteById(movieId);
		
		doGetMovieList(request, response);
	}

	private void doGetMovieList(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String movieNameKey = request.getParameter("movieNameKey");//"" "值"
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		Map<String, String> params = new HashMap<String, String>();
		params.put("movieNameKey", movieNameKey);
		params.put("pageNum", pageNum);
		params.put("pageSize", pageSize);
		PageInfo<Movie> pageInfo = movieService.getBy(params);
		for (Movie movie : pageInfo.getData()) {
			String type = movie.getMovieType().replaceAll("电影", "");
			movie.setMovieType(type);
		}
		
		// 利用第三方类库,将数据转换成JSON字符串
		Gson gson = new Gson();
		String jsonObject = gson.toJson(pageInfo);
		// 2.响应
		response.setContentType("application/json");
		response.getWriter().write(jsonObject);
	}

}
